import sakura from './sakura'
import * as math from 'mathjs'
import env from './env'

/**
 * @func noRepeat
 * @desc 对象型数组去重
 * @param {Array} arr 需要去重的数组
 * @param {String} prop 按哪个属性去重
 * @return {Array} 去重后的新数组
 */
const noRepeat = (arr, prop) => {
  const obj = {}
  const newArr = arr.reduce((item, next) => {
    if (!obj[next[prop]]) {
      obj[next[prop]] = true
      item.push(next)
    }
    return item
  }, [])
  return newArr
}

/**
 * @func treeToFlat
 * @desc 数组扁平化
 * @param {arr}  Array
 * @param {optionProps}  Object
 * @return {flatArr} Array
 */

const optionProps = {
  label: 'name',
  value: 'id',
  pid: 'pid',
  children: 'children',
}

const treeToFlat = (arr, optionProps) => {
  const flatArr = []
  arr.forEach(item => {
    const obj = {
      [optionProps.label]: item[optionProps.label],
      [optionProps.value]: item[optionProps.value],
    }
    flatArr.push(obj)

    if (item[optionProps.children]?.length) {
      flatArr.push(...treeToFlat(item[optionProps.children], optionProps))
    }
  })

  return flatArr
}

// const list = [
//   { id: '/', name: 'Home' },
//   { id: '/dashboard', name: 'Dashboard', pid: '/' },
//   { id: '/overview', name: 'Overview', pid: '/dashboard' },
//   { id: '/analytics', name: 'Analytics', pid: '/dashboard' },
//   { id: '/settings', name: 'Settings', pid: '/' },
//   { id: '/about', name: 'About' },
//   { id: '/A', name: 'About', pid: '/overview' },
//   { id: '/B', name: 'About', pid: '/overview' },
// ]

/**
 * @func arrayToTree
 * @desc 扁平化数据处理成多维数组
 * @param {arr}  Array
 * @param {optionProps}  Object
 * @return {flatArr} Array
 */

function arrayToTree(data) {
  // 创建一个id映射，用来快速查找元素
  const idMap = {}
  // 遍历数组，初始化每个元素的children字段，并将其存入idMap
  data.forEach(item => {
    idMap[item.id] = { ...item, children: [] }
  })

  // 初始化一个数组，用来存储根节点
  const treeList = []

  // 再次遍历数组，根据pid建立树结构
  data.forEach(item => {
    const { id, pid } = item
    if (!pid) {
      // 如果pid为null或undefined，表示是根节点
      treeList.push(idMap[id])
    } else {
      // 否则，将当前元素添加到其父元素的children字段中
      if (idMap[pid]) {
        idMap[pid].children.push(idMap[id])
      }
    }
  })

  return treeList
}

/**
 * @func isIOS
 * @desc 判断是否IOS
 * @param {}
 * @return {Boolean} 是否为IOS
 */
export const isIOS = () => {
  return /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase())
}

/**
 * @func getUserInfo
 * @desc 获取用户信息
 * @param {}
 * @return {String}
 */
const getUserInfo = () => {
  return window.localStorage.getItem('username')
}

/* math */
const mathComputed = {
  // 加
  add(num1, num2) {
    return math.add(math.bignumber(num1), math.bignumber(num2))
  },

  // 减
  subtract(num1, num2) {
    return math.subtract(math.bignumber(num1), math.bignumber(num2))
  },

  // 乘
  multiply(num1, num2) {
    return math.multiply(math.bignumber(num1), math.bignumber(num2))
  },

  // 除
  divide(num1, num2) {
    return math.divide(math.bignumber(num1), math.bignumber(num2))
  },
}

export default {
  noRepeat,
  treeToFlat,
  arrayToTree,
  sakura,
  getUserInfo,
  ...mathComputed,
  env,
}
